(function() {

    //滚动
    function smb_Scroll(wapper, options) {
        var scrollCall,
            scrollStartCall,
            scrollEndCall;
        if (options) {
            scrollStartCall = options.scrollStartCall;
            scrollCall = options.scrollCall;
            scrollEndCall = options.scrollEndCall;
        } else {
            options = {
                scrollBarY : true,
                scrollBarX : false
            };
        }
        var params = $.extend({}, {
            scrollBarFade : true
        }, options);
        var myScroll = new JRoll(wapper, params);

        document.addEventListener('touchmove', function(e) {
            e.preventDefault();
        }, false);
        myScroll.on('scrollStart', function() {// 滚动结束
            if (scrollStartCall)
                scrollStartCall(this);
        });
        myScroll.on('scroll', function() {// 滚动进行时
            if (scrollCall)
                scrollCall(this);
        });
        myScroll.on('scrollEnd', function() {// 滚动结束
            if (scrollEndCall)
                scrollEndCall(this);
        });
        return myScroll;
    }

    var iosSelectUtil = {
        isArray : function(arg1) {
            return Object.prototype.toString.call(arg1) === '[object Array]';
        },
        isFunction : function(arg1) {
            return typeof arg1 === 'function';
        },
        attrToData : function(dom, index) {
            var obj = {
                dom : dom,
                atindex : index,
                id : $(dom).data('id'),
                value : $(dom).data('value')
            };
            return obj;
        },
        attrToHtml : function(obj) {
            var html = '';
            for (var p in obj) {
                html += 'data-' + p + '="' + obj[p] + '"';
            }
            return html;
        },
        offset : function(el) {
            var left = -el.offsetLeft,
                top = -el.offsetTop;

            // jshint -W084
            while ( el = el.offsetParent) {
                left -= el.offsetLeft;
                top -= el.offsetTop;
            }
            // jshint +W084

            return {
                left : left,
                top : top
            };
        },
        scrollToElement : function(scroller, el, time, offsetX, offsetY, easing) {
            var wrapper = scroller.wrapper;
            el = el.nodeType ? el : wrapper.querySelector(el);

            if (!el) {
                return;
            }

            var pos = iosSelectUtil.offset(el);
            var wrapperOffset = iosSelectUtil.offset(wrapper);

            pos.left -= wrapperOffset.left;
            pos.top -= wrapperOffset.top;

            // if offsetX/Y are true we center the element to the screen
            if (offsetX === true) {
                offsetX = Math.round(el.offsetWidth / 2 - wrapper.offsetWidth / 2);
            }
            if (offsetY === true) {
                offsetY = Math.round(el.offsetHeight / 2 - wrapper.offsetHeight / 2);
            }

            pos.left -= offsetX || 0;
            pos.top -= offsetY || 0;

            pos.left = pos.left > 0 ? 0 : pos.left < scroller.maxScrollX ? scroller.maxScrollX : pos.left;
            pos.top = pos.top > 0 ? 0 : pos.top < scroller.maxScrollY ? scroller.maxScrollY : pos.top;

            time = time === undefined || time === null || time === 'auto' ? Math.max(Math.abs(scroller.x - pos.left), Math.abs(scroller.y - pos.top)) : time;

            scroller.scrollTo(pos.left, pos.top, time, easing);
        },
    };
    // Layer
    function Layer(html, opts) {
        if (!(this instanceof Layer)) {
            return new Layer(html, opts);
        }
        this.html = html;
        this.opts = opts;
        var el = $(document.createElement('div'));
        el.addClass('olay').css("z-index", 99999);
        // var layer_el = $('<div class="layer"></div>');
        var layer_el = $(document.createElement('div'));
        layer_el.addClass('layer ub-f1 ub-ver');
        this.el = el;
        this.layer_el = layer_el;
        this.init();
    }


    Layer.prototype = {
        init : function() {
            this.layer_el.html(this.html);
            if (this.opts.container && $(this.opts.container)) {
                $(this.opts.container).append(this.el);
            } else {
                $(document.body).append(this.el);
            }
            this.el.append(this.layer_el);
            this.el.css('height', Math.max(document.documentElement.getBoundingClientRect().height, window.innerHeight));
            if (this.opts.className) {
                this.el.addClass(this.opts.className);
            }
            this.bindEvent();
        },
        bindEvent : function() {
            var sureDom = $('.sure', this.el);
            var closeDom = $('.close', this.el);
            var self = this;

            // this.el.click(function(e) {
            // self.close();
            // self.opts.fallback && self.opts.fallback();
            // })

            this.layer_el.click(function(e) {
                e.stopPropagation();
            });
            for (var i = 0,
                len = sureDom.length; i < len; i++) {
                $(sureDom[i]).click(function(e) {
                    self.close();
                });
            }
            for (var i = 0,
                len = closeDom.length; i < len; i++) {
                $(closeDom[i]).click(function(e) {
                    self.close();
                    self.opts.fallback && self.opts.fallback();
                });
            }
        },
        close : function() {
            var self = this;
            if (self.el) {
                if (self.opts.showAnimate) {
                    self.el.addClass('fadeOutDown');
                    setTimeout(function() {
                        self.removeDom();
                    }, 500);
                } else {
                    self.removeDom();
                }
            }
        },
        removeDom : function() {
            this.el.remove();
            this.el = null;
            if ($(document.body).hasClass('ios-select-body-class')) {
                $(document.body).removeClass('ios-select-body-class');
            };
        }
    }
    function IosSelect(level, data, options) {
        if (!iosSelectUtil.isArray(data) || data.length === 0) {
            return;
        }
        this.data = data;
        this.level = level || 1;
        this.options = options;
        this.typeBox = 'one-level-box';
        if (this.level === 1) {
            this.typeBox = 'one-level-box';
        } else if (this.level === 2) {
            this.typeBox = 'two-level-box';
        } else if (this.level === 3) {
            this.typeBox = 'three-level-box';
        } else if (this.level === 4) {
            this.typeBox = 'four-level-box';
        } else if (this.level === 5) {
            this.typeBox = 'five-level-box';
        }
        this.callback = options.callback;
        this.fallback = options.fallback;
        this.title = options.title || '';

        this.options.itemShowCount = [3, 5, 7, 9].indexOf(options.itemShowCount) !== -1 ? options.itemShowCount : 7;
        this.options.coverArea1Top = Math.floor(this.options.itemShowCount / 2);
        this.options.coverArea2Top = Math.ceil(this.options.itemShowCount / 2);
        this.options.headerHeight = options.headerHeight || 44;

        var pageHeight = $("body").offset().height;
        //totalHeight -> px
        this.options.totalHeight = options.totalHeight || pageHeight * 0.4;
        this.options.itemHeight = this.options.totalHeight / this.options.itemShowCount;

        this.options.relation = iosSelectUtil.isArray(this.options.relation) ? this.options.relation : [];
        this.options.oneTwoRelation = this.options.relation[0];
        this.options.twoThreeRelation = this.options.relation[1];
        this.options.threeFourRelation = this.options.relation[2];
        this.options.fourFiveRelation = this.options.relation[3];
        this.options.cssUnit = 'px';
        this.setBase();
        this.init();
    };

    IosSelect.prototype = {
        init : function() {
            this.initLayer();
            // 选中元素的信息
            this.selectOneObj = {};
            this.selectTwoObj = {};
            this.selectThreeObj = {};
            this.selectFourObj = {};
            this.selectFiveObj = {};
            this.setOneLevel(this.options.oneLevelId, this.options.twoLevelId, this.options.threeLevelId, this.options.fourLevelId, this.options.fiveLevelId);
        },
        initLayer : function() {
            var self = this;
            var alls = ['<div class="iosselect-header eco-picker-header ub uinn">', '   <div class="nav-btn sc-text-active close">取消</div>', '   <h2 id="iosSelectTitle" class="eco-picker-title ut ub-f1 ulev-3 ut-s tx-c"></h2>', '   <div class="nav-btn sc-text-active sure">确定</div>', '</div>', '<section class="iosselect-box ub-f1">', '   <div class="one-level-contain" id="oneLevelContain">', '       <ul class="select-one-level">', '       </ul>', '   </div>', '   <div class="two-level-contain" id="twoLevelContain">', '       <ul class="select-two-level">', '       </ul>', '   </div>', '   <div class="three-level-contain" id="threeLevelContain">', '       <ul class="select-three-level">', '       </ul>', '   </div>', '   <div class="four-level-contain" id="fourLevelContain">', '       <ul class="select-four-level">', '       </ul>', '   </div>', '   <div class="five-level-contain" id="fiveLevelContain">', '       <ul class="select-five-level">', '       </ul>', '   </div>', '</section>', '<hr class="cover-area1"/>', '<hr class="cover-area2"/>', '<div class="ios-select-loading-box" id="iosSelectLoadingBox">', '   <div class="ios-select-loading"></div>', '</div>'];
            var all_html = alls.join('\r\n');
            this.iosSelectLayer = new Layer(all_html, {
                className : 'ios-select-widget-box eco-picker-widget ' + this.typeBox + (this.options.addClassName ? ' ' + this.options.addClassName : '') + (this.options.showAnimate ? ' fadeInUp' : ''),
                container : this.options.container || '',
                showAnimate : this.options.showAnimate,
                fallback : this.options.fallback
            });

            this.iosSelectTitleDom = $('#iosSelectTitle', this.iosSelectLayer.el);
            this.iosSelectLoadingBoxDom = $('#iosSelectLoadingBox', this.iosSelectLayer.el);
            if (this.options.title) {
                this.iosSelectTitleDom.html(this.options.title);
            }
            this.iosSelectHeaderDom = $('.eco-picker-header', this.iosSelectLayer.el);
            this.options.headerHeight = this.iosSelectHeaderDom.offset().height;

            if (this.options.headerHeight && this.options.itemHeight) {
                this.coverArea1Dom = $('.cover-area1', this.iosSelectLayer.el);
                this.coverArea1Dom.css('top', this.options.headerHeight + this.options.itemHeight * this.options.coverArea1Top + this.options.cssUnit);

                this.coverArea2Dom = $('.cover-area2', this.iosSelectLayer.el);
                this.coverArea2Dom.css('top', this.options.headerHeight + this.options.itemHeight * this.options.coverArea2Top + this.options.cssUnit);
            }

            this.oneLevelContainDom = $('#oneLevelContain', this.iosSelectLayer.el);
            this.twoLevelContainDom = $('#twoLevelContain', this.iosSelectLayer.el);
            this.threeLevelContainDom = $('#threeLevelContain', this.iosSelectLayer.el);
            this.fourLevelContainDom = $('#fourLevelContain', this.iosSelectLayer.el);
            this.fiveLevelContainDom = $('#fiveLevelContain', this.iosSelectLayer.el);

            this.oneLevelUlContainDom = $('.select-one-level', this.iosSelectLayer.el);
            this.twoLevelUlContainDom = $('.select-two-level', this.iosSelectLayer.el);
            this.threeLevelUlContainDom = $('.select-three-level', this.iosSelectLayer.el);
            this.fourLevelUlContainDom = $('.select-four-level', this.iosSelectLayer.el);
            this.fiveLevelUlContainDom = $('.select-five-level', this.iosSelectLayer.el);

            $('.layer', this.iosSelectLayer.el).css('height', this.options.itemHeight * this.options.itemShowCount + this.options.headerHeight + this.options.cssUnit);

            this.oneLevelContainDom.css('height', this.options.itemHeight * this.options.itemShowCount + this.options.cssUnit);

            this.offsetTop = document.body.scrollTop;
            $(document.body).addClass('ios-select-body-class');
            window.scrollTo(0, 0);

            this.scrollOne = smb_Scroll('#oneLevelContain', {
                scrollStartCall : function(obj) {
                    self.toggleClassList(self.oneLevelContainDom);
                },
                scrollCall : function(obj) {
                    var pa = Math.abs(obj.y / self.baseSize) / self.options.itemHeight;
                    var plast = 1;

                    plast = Math.round(pa) + 1;
                    self.toggleClassList(self.oneLevelContainDom);

                    self.changeClassName(self.oneLevelContainDom, plast);
                },
                scrollEndCall : function(obj) {
                    var pa = Math.abs(obj.y / self.baseSize) / self.options.itemHeight;
                    var plast = 1;
                    var to = 0;
                    if (Math.ceil(pa) === Math.round(pa)) {
                        to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
                        plast = Math.ceil(pa) + 1;
                    } else {
                        to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
                        plast = Math.floor(pa) + 1;
                    }
                    self.scrollOne.scrollTo(0, -to, 0);

                    self.toggleClassList(self.oneLevelContainDom);

                    var pdom = self.changeClassName(self.oneLevelContainDom, plast);

                    self.selectOneObj = iosSelectUtil.attrToData(pdom, plast);

                    if (self.level > 1 && self.options.oneTwoRelation === 1) {
                        self.setTwoLevel(self.selectOneObj.id, self.selectTwoObj.id, self.selectThreeObj.id, self.selectFourObj.id, self.selectFiveObj.id);
                    }
                }
            });
            if (this.level >= 2) {
                this.twoLevelContainDom.css('height', this.options.itemHeight * this.options.itemShowCount + this.options.cssUnit);
                this.scrollTwo = smb_Scroll('#twoLevelContain', {
                    scrollStartCall : function(obj) {
                        self.toggleClassList(self.twoLevelContainDom);
                    },
                    scrollCall : function(obj) {
                        var pa = Math.abs(obj.y / self.baseSize) / self.options.itemHeight;
                        var plast = 0;
                        plast = Math.round(pa) + 1;

                        self.toggleClassList(self.twoLevelContainDom);
                        self.changeClassName(self.twoLevelContainDom, plast);
                    },
                    scrollEndCall : function(obj) {
                        var pa = Math.abs(obj.y / self.baseSize) / self.options.itemHeight;
                        var plast = 1;
                        var to = 0;
                        if (Math.ceil(pa) === Math.round(pa)) {
                            to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
                            plast = Math.ceil(pa) + 1;
                        } else {
                            to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
                            plast = Math.floor(pa) + 1;
                        }
                        self.scrollTwo.scrollTo(0, -to, 0);

                        self.toggleClassList(self.twoLevelContainDom);

                        var pdom = self.changeClassName(self.twoLevelContainDom, plast);

                        self.selectTwoObj = iosSelectUtil.attrToData(pdom, plast);

                        if (self.level > 2 && self.options.twoThreeRelation === 1) {
                            self.setThreeLevel(self.selectOneObj.id, self.selectTwoObj.id, self.selectThreeObj.id, self.selectFourObj.id, self.selectFiveObj.id);
                        }
                    }
                });
            }
            if (this.level >= 3) {
                this.threeLevelContainDom.css('height', this.options.itemHeight * this.options.itemShowCount + this.options.cssUnit);
                this.scrollThree = smb_Scroll('#threeLevelContain', {
                    scrollStartCall : function(obj) {
                        self.toggleClassList(self.threeLevelContainDom);
                    },
                    scrollCall : function(obj) {
                        var pa = Math.abs(obj.y / self.baseSize) / self.options.itemHeight;
                        var plast = 0;
                        plast = Math.round(pa) + 1;

                        self.toggleClassList(self.threeLevelContainDom);

                        self.changeClassName(self.threeLevelContainDom, plast);
                    },
                    scrollEndCall : function(obj) {
                        var pa = Math.abs(obj.y / self.baseSize) / self.options.itemHeight;
                        var plast = 1;
                        var to = 0;
                        if (Math.ceil(pa) === Math.round(pa)) {
                            to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
                            plast = Math.ceil(pa) + 1;
                        } else {
                            to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
                            plast = Math.floor(pa) + 1;
                        }
                        self.scrollThree.scrollTo(0, -to, 0);

                        self.toggleClassList(self.threeLevelContainDom);

                        var pdom = self.changeClassName(self.threeLevelContainDom, plast);

                        self.selectThreeObj = iosSelectUtil.attrToData(pdom, plast);
                        if (self.level >= 4 && self.options.threeFourRelation === 1) {
                            self.setFourLevel(self.selectOneObj.id, self.selectTwoObj.id, self.selectThreeObj.id, self.selectFourObj.id, self.selectFiveObj.id);
                        }
                    }
                });
            }
            if (this.level >= 4) {
                this.fourLevelContainDom.css('height', this.options.itemHeight * this.options.itemShowCount + this.options.cssUnit);
                this.scrollFour = smb_Scroll('#fourLevelContain', {
                    scrollStartCall : function(obj) {
                        self.toggleClassList(self.fourLevelContainDom);
                    },
                    scrollCall : function(obj) {
                        var pa = Math.abs(obj.y / self.baseSize) / self.options.itemHeight;
                        var plast = 0;
                        plast = Math.round(pa) + 1;

                        self.toggleClassList(self.fourLevelContainDom);

                        self.changeClassName(self.fourLevelContainDom, plast);
                    },
                    scrollEndCall : function(obj) {
                        var pa = Math.abs(obj.y / self.baseSize) / self.options.itemHeight;
                        var plast = 1;
                        var to = 0;
                        if (Math.ceil(pa) === Math.round(pa)) {
                            to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
                            plast = Math.ceil(pa) + 1;
                        } else {
                            to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
                            plast = Math.floor(pa) + 1;
                        }
                        self.scrollFour.scrollTo(0, -to, 0);

                        self.toggleClassList(self.fourLevelContainDom);

                        var pdom = self.changeClassName(self.fourLevelContainDom, plast);

                        self.selectFourObj = iosSelectUtil.attrToData(pdom, plast);

                        if (self.level >= 5 && self.options.fourFiveRelation === 1) {
                            self.setFiveLevel(self.selectOneObj.id, self.selectTwoObj.id, self.selectThreeObj.id, self.selectFourObj.id, self.selectFiveObj.id);
                        }
                    }
                });
            }
            if (this.level >= 5) {
                this.fiveLevelContainDom.css('height', this.options.itemHeight * this.options.itemShowCount + this.options.cssUnit);
                this.scrollFive = smb_Scroll('#fiveLevelContain', {
                    scrollStartCall : function(obj) {
                        self.toggleClassList(self.fiveLevelContainDom);
                    },
                    scrollCall : function(obj) {
                        var pa = Math.abs(obj.y / self.baseSize) / self.options.itemHeight;
                        var plast = 0;
                        plast = Math.round(pa) + 1;

                        self.toggleClassList(self.fiveLevelContainDom);

                        self.changeClassName(self.fiveLevelContainDom, plast);
                    },
                    scrollEndCall : function(obj) {
                        var pa = Math.abs(obj.y / self.baseSize) / self.options.itemHeight;
                        var plast = 1;
                        var to = 0;
                        if (Math.ceil(pa) === Math.round(pa)) {
                            to = Math.ceil(pa) * self.options.itemHeight * self.baseSize;
                            plast = Math.ceil(pa) + 1;
                        } else {
                            to = Math.floor(pa) * self.options.itemHeight * self.baseSize;
                            plast = Math.floor(pa) + 1;
                        }
                        self.scrollFive.scrollTo(0, -to, 0);

                        self.toggleClassList(self.fiveLevelContainDom);

                        var pdom = self.changeClassName(self.fiveLevelContainDom, plast);

                        self.selectFiveObj = iosSelectUtil.attrToData(pdom, plast);
                    }
                });
            }

            // 取消 确认 事件
            this.closeBtnDom = $('.close', this.iosSelectLayer.el);
            this.closeBtnDom.click(function(e) {
                window.scrollTo(0, self.offsetTop);
            });

            this.selectBtnDom = $('.sure', this.iosSelectLayer.el);
            this.selectBtnDom.click(function(e) {
                window.scrollTo(0, self.offsetTop);
                self.callback && self.callback(self.selectOneObj, self.selectTwoObj, self.selectThreeObj, self.selectFourObj, self.selectFiveObj);
            });
        },
        loadingShow : function() {
            this.options.showLoading && (this.iosSelectLoadingBoxDom.css('display', 'block'));
        },
        loadingHide : function() {
            this.iosSelectLoadingBoxDom.css('display', 'none');
        },
        getOneLevel : function() {
            return this.data[0];
        },
        setOneLevel : function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId) {
            if (iosSelectUtil.isArray(this.data[0])) {
                var oneLevelData = this.getOneLevel();
                this.renderOneLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, oneLevelData);
            } else if (iosSelectUtil.isFunction(this.data[0])) {
                this.loadingShow();
                this.data[0]( function(oneLevelData) {
                    this.loadingHide();
                    this.renderOneLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, oneLevelData);
                }.bind(this));
            } else {
                throw new Error('data format error');
            }
        },
        renderOneLevel : function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, oneLevelData) {
            var hasAtId = oneLevelData.some(function(v, i, o) {
                return v.id == oneLevelId;
            });
            if (!hasAtId) {
                oneLevelId = oneLevelData[0]['id'];
            }
            var oneHtml = '';
            var self = this;
            var plast = 0;
            oneHtml += this.getWhiteItem();
            oneLevelData.forEach( function(v, i, o) {
                if (v.id == oneLevelId) {
                    oneHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit + ';" ' + iosSelectUtil.attrToHtml(v) + ' class="at">' + v.value + '</li>';
                    plast = i + 1;
                } else {
                    oneHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit + ';"' + iosSelectUtil.attrToHtml(v) + '>' + v.value + '</li>';
                }
            }.bind(this));
            oneHtml += this.getWhiteItem();
            this.oneLevelUlContainDom.html(oneHtml);

            this.scrollOne.refresh();

            iosSelectUtil.scrollToElement(this.scrollOne, 'li:nth-child(' + plast + ')', 0);
            if (this.level >= 2) {
                this.setTwoLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId);
            }

            var pdom = this.changeClassName(this.oneLevelContainDom, plast);
            this.selectOneObj = iosSelectUtil.attrToData(pdom, this.getAtIndexByPlast(plast));
        },
        getTwoLevel : function(oneLevelId) {
            var twoLevelData = [];
            if (this.options.oneTwoRelation === 1) {
                this.data[1].forEach(function(v, i, o) {
                    if (v['parentId'] == oneLevelId) {
                        twoLevelData.push(v);
                    }
                });
            } else {
                twoLevelData = this.data[1];
            }
            return twoLevelData;
        },
        setTwoLevel : function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId) {
            if (iosSelectUtil.isArray(this.data[1])) {
                var twoLevelData = this.getTwoLevel(oneLevelId);
                this.renderTwoLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, twoLevelData);
            } else if (iosSelectUtil.isFunction(this.data[1])) {
                this.loadingShow();
                this.data[1](oneLevelId, function(twoLevelData) {
                    this.loadingHide();
                    this.renderTwoLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, twoLevelData);
                }.bind(this));
            } else {
                throw new Error('data format error');
            }
        },
        renderTwoLevel : function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, twoLevelData) {
            var plast = 0;
            var hasAtId = twoLevelData.some(function(v, i, o) {
                return v.id == twoLevelId;
            });
            if (!hasAtId) {
                twoLevelId = twoLevelData[0]['id'];
            }
            var twoHtml = '';
            var self = this;
            twoHtml += this.getWhiteItem();
            twoLevelData.forEach( function(v, i, o) {
                if (v.id == twoLevelId) {
                    twoHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit + ';"' + iosSelectUtil.attrToHtml(v) + ' class="at">' + v.value + '</li>';
                    plast = i + 1;
                } else {
                    twoHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit + ';"' + iosSelectUtil.attrToHtml(v) + '>' + v.value + '</li>';
                }
            }.bind(this));
            twoHtml += this.getWhiteItem();
            this.twoLevelUlContainDom.html(twoHtml);
            this.scrollTwo.refresh();

            iosSelectUtil.scrollToElement(this.scrollTwo, 'li:nth-child(' + plast + ')', 0);
            if (this.level >= 3) {
                this.setThreeLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId);
            }

            var pdom = this.changeClassName(this.twoLevelContainDom, plast);
            this.selectTwoObj = iosSelectUtil.attrToData(pdom, this.getAtIndexByPlast(plast));
        },
        getThreeLevel : function(oneLevelId, twoLevelId) {
            var threeLevelData = [];
            if (this.options.twoThreeRelation === 1) {
                this.data[2].forEach(function(v, i, o) {
                    if (v['parentId'] == twoLevelId) {
                        threeLevelData.push(v);
                    }
                });
            } else {
                threeLevelData = this.data[2];
            }
            return threeLevelData;
        },
        setThreeLevel : function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId) {
            if (iosSelectUtil.isArray(this.data[2])) {
                var threeLevelData = this.getThreeLevel(oneLevelId, twoLevelId);
                this.renderThreeLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, threeLevelData);
            } else if (iosSelectUtil.isFunction(this.data[2])) {
                this.loadingShow();
                this.data[2](oneLevelId, twoLevelId, function(threeLevelData) {
                    this.loadingHide();
                    this.renderThreeLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, threeLevelData);
                }.bind(this));
            } else {
                throw new Error('data format error');
            }
        },
        renderThreeLevel : function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, threeLevelData) {
            var plast = 0;
            var hasAtId = threeLevelData.some(function(v, i, o) {
                return v.id == threeLevelId;
            });
            if (!hasAtId) {
                threeLevelId = threeLevelData[0]['id'];
            }
            var threeHtml = '';
            var self = this;
            threeHtml += this.getWhiteItem();
            threeLevelData.forEach( function(v, i, o) {
                if (v.id == threeLevelId) {
                    threeHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit + ';"' + iosSelectUtil.attrToHtml(v) + ' class="at">' + v.value + '</li>';
                    plast = i + 1;
                } else {
                    threeHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit + ';"' + iosSelectUtil.attrToHtml(v) + '>' + v.value + '</li>';
                }
            }.bind(this));
            threeHtml += this.getWhiteItem();
            this.threeLevelUlContainDom.html(threeHtml);
            this.scrollThree.refresh();
            iosSelectUtil.scrollToElement(this.scrollThree, 'li:nth-child(' + plast + ')', 0);
            if (this.level >= 4) {
                this.setFourLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId);
            }

            var pdom = this.changeClassName(this.threeLevelContainDom, plast);
            this.selectThreeObj = iosSelectUtil.attrToData(pdom, this.getAtIndexByPlast(plast));
        },
        getFourLevel : function(oneLevelId, twoLevelId, threeLevelId) {
            var fourLevelData = [];
            if (this.options.threeFourRelation === 1) {
                this.data[3].forEach(function(v, i, o) {
                    if (v['parentId'] == threeLevelId) {
                        fourLevelData.push(v);
                    }
                });
            } else {
                fourLevelData = this.data[3];
            }
            return fourLevelData;
        },
        setFourLevel : function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId) {
            if (iosSelectUtil.isArray(this.data[3])) {
                var fourLevelData = this.getFourLevel(oneLevelId, twoLevelId, threeLevelId);
                this.renderFourLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, fourLevelData);
            } else if (iosSelectUtil.isFunction(this.data[3])) {
                this.loadingShow();
                this.data[3](oneLevelId, twoLevelId, threeLevelId, function(fourLevelData) {
                    this.loadingHide();
                    this.renderFourLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, fourLevelData);
                }.bind(this));
            } else {
                throw new Error('data format error');
            }
        },
        renderFourLevel : function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, fourLevelData) {
            var plast = 0;
            var hasAtId = fourLevelData.some(function(v, i, o) {
                return v.id == fourLevelId;
            });
            if (!hasAtId) {
                fourLevelId = fourLevelData[0]['id'];
            }
            var fourHtml = '';
            var self = this;
            fourHtml += this.getWhiteItem();
            fourLevelData.forEach( function(v, i, o) {
                if (v.id == fourLevelId) {
                    fourHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit + ';"' + iosSelectUtil.attrToHtml(v) + ' class="at">' + v.value + '</li>';
                    plast = i + 1;
                } else {
                    fourHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit + ';"' + iosSelectUtil.attrToHtml(v) + '>' + v.value + '</li>';
                }
            }.bind(this));
            fourHtml += this.getWhiteItem();
            this.fourLevelUlContainDom.html(fourHtml);
            this.scrollFour.refresh();
            iosSelectUtil.scrollToElement(this.scrollFour, 'li:nth-child(' + plast + ')', 0);

            if (this.level >= 5) {
                this.setFiveLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId);
            }

            var pdom = this.changeClassName(this.fourLevelContainDom, plast);
            this.selectFourObj = iosSelectUtil.attrToData(pdom, this.getAtIndexByPlast(plast));
        },
        getFiveLevel : function(oneLevelId, twoLevelId, threeLevelId, fourLevelId) {
            var fiveLevelData = [];
            if (this.options.fourFiveRelation === 1) {
                this.data[4].forEach(function(v, i, o) {
                    if (v['parentId'] == fourLevelId) {
                        fiveLevelData.push(v);
                    }
                });
            } else {
                fiveLevelData = this.data[4];
            }
            return fiveLevelData;
        },
        setFiveLevel : function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId) {
            if (iosSelectUtil.isArray(this.data[4])) {
                var fiveLevelData = this.getFiveLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId);
                this.renderFiveLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, fiveLevelData);
            } else if (iosSelectUtil.isFunction(this.data[4])) {
                this.loadingShow();
                this.data[4](oneLevelId, twoLevelId, threeLevelId, fourLevelId, function(fiveLevelData) {
                    this.loadingHide();
                    this.renderFiveLevel(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, fiveLevelData);
                }.bind(this));
            } else {
                throw new Error('data format error');
            }
        },
        renderFiveLevel : function(oneLevelId, twoLevelId, threeLevelId, fourLevelId, fiveLevelId, fiveLevelData) {
            var plast = 0;
            var hasAtId = fiveLevelData.some(function(v, i, o) {
                return v.id == fiveLevelId;
            });
            if (!hasAtId) {
                fiveLevelId = fiveLevelData[0]['id'];
            }
            var fiveHtml = '';
            var self = this;
            fiveHtml += this.getWhiteItem();
            fiveLevelData.forEach( function(v, i, o) {
                if (v.id == fiveLevelId) {
                    fiveHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit + ';"' + iosSelectUtil.attrToHtml(v) + ' class="at">' + v.value + '</li>';
                    plast = i + 1;
                } else {
                    fiveHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit + ';"' + iosSelectUtil.attrToHtml(v) + '>' + v.value + '</li>';
                }
            }.bind(this));
            fiveHtml += this.getWhiteItem();
            this.fiveLevelUlContainDom.html(fiveHtml);
            this.scrollFive.refresh();
            iosSelectUtil.scrollToElement(this.scrollFive, 'li:nth-child(' + plast + ')', 0);
            var pdom = this.changeClassName(this.fiveLevelContainDom, plast);
            this.selectFiveObj = iosSelectUtil.attrToData(pdom, this.getAtIndexByPlast(plast));
        },
        getWhiteItem : function() {
            var whiteItemHtml = '';
            whiteItemHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit + '"></li>';
            if (this.options.itemShowCount > 3) {
                whiteItemHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit + '"></li>';
            }
            if (this.options.itemShowCount > 5) {
                whiteItemHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit + '"></li>';
            }
            if (this.options.itemShowCount > 7) {
                whiteItemHtml += '<li style="height: ' + this.options.itemHeight + this.options.cssUnit + '; line-height: ' + this.options.itemHeight + this.options.cssUnit + '"></li>';
            }
            return whiteItemHtml;
        },
        changeClassName : function(levelContainDom, plast) {
            var pdom;
            if (this.options.itemShowCount === 3) {
                pdom = $('li:nth-child(' + (plast + 1) + ')', levelContainDom);
                pdom.addClass('at');
            } else if (this.options.itemShowCount === 5) {
                pdom = $('li:nth-child(' + (plast + 2) + ')', levelContainDom);
                pdom.addClass('at');

                $('li:nth-child(' + (plast + 1) + ')', levelContainDom).addClass('side1');
                $('li:nth-child(' + (plast + 3) + ')', levelContainDom).addClass('side1');
            } else if (this.options.itemShowCount === 7) {
                pdom = $('li:nth-child(' + (plast + 3) + ')', levelContainDom);
                pdom.addClass('at');

                $('li:nth-child(' + (plast + 2) + ')', levelContainDom).addClass('side1');
                $('li:nth-child(' + (plast + 1) + ')', levelContainDom).addClass('side2');
                $('li:nth-child(' + (plast + 4) + ')', levelContainDom).addClass('side1');
                $('li:nth-child(' + (plast + 5) + ')', levelContainDom).addClass('side2');
            } else if (this.options.itemShowCount === 9) {
                pdom = $('li:nth-child(' + (plast + 4) + ')', levelContainDom);
                pdom.addClass('at');

                $('li:nth-child(' + (plast + 3) + ')', levelContainDom).addClass('side1');
                $('li:nth-child(' + (plast + 2) + ')', levelContainDom).addClass('side2');
                $('li:nth-child(' + (plast + 5) + ')', levelContainDom).addClass('side1');
                $('li:nth-child(' + (plast + 6) + ')', levelContainDom).addClass('side2');
            }
            return pdom;
        },
        getAtIndexByPlast : function(plast) {
            return plast + Math.ceil(this.options.itemShowCount / 2);
        },
        setBase : function() {
            this.baseSize = 1;
        },
        toggleClassList : function(dom) {
            Array.prototype.slice.call($('li', dom)).forEach(function(v) {
                if ($(v).hasClass('at')) {
                    $(v).removeClass('at');
                } else if ($(v).hasClass('side1')) {
                    $(v).removeClass('side1');
                } else if ($(v).hasClass('side2')) {
                    $(v).removeClass('side2');
                }
            })
        }
    }
    if ( typeof module != 'undefined' && module.exports) {
        module.exports = IosSelect;
    } else if ( typeof define == 'function' && define.amd) {
        define(function() {
            return IosSelect;
        });
    } else {
        window.IosSelect = IosSelect;
    }
})();
